<template>
  <div></div>
</template>
<script >
import { getAction } from '@/api/manage'
import { mapActions } from 'vuex'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'
import Vue from 'vue'
// import { ACCESS_TOKEN } from '@/store/mutation-types'
// import Vue from 'vue'
export default {
  data() {
    return {
      url: {
        queryAllDictItems: 'sys/dict/queryAllDictItems'
      }
    }
  },
  created() {
    const code = this.$route.query.code
    let num = this.$route.query.num || 0
    if (code) {
      this.toPage()
      return false
    }
    if (num > 2) {
      console.error('跳转出错了')
      return false
    }
    const origin = window.location.origin
    // getAction('/feishu/auth/getAuthUrl', { url: encodeURI(`http://121.5.252.231:9009/getToken?url=${this.$route.query.url}&num=${num}`) }).then(res => {
    //   num++
    //   // window.open(res.result)
    //   window.location.origin = res.result
    // })
    getAction('/feishu/auth/getAuthUrl', { url: encodeURI(`${origin}/getToken?url=${this.$route.query.url}&num=${num}`) }).then(res => {
      num++
      // window.open(res.result)
      window.location.href = res.result
    })
  },
  methods: {
    ...mapActions(['ThirdLogin']),
    getUser() {
      return getAction('/sys/user/userinfo')
    },
    getDict() {
      return getAction(this.url.queryAllDictItems).then((res) => {
        if (res.success) {
          Vue.ls.remove(UI_CACHE_DB_DICT_DATA)
          Vue.ls.set(UI_CACHE_DB_DICT_DATA, res.result, 7 * 24 * 60 * 60 * 1000)
        }
      })
    },
    async toPage() {
      const data = await this.getUser()
      if (data.success) {
        const token = data.result.token
        const url = this.$route.query.url
        await this.ThirdLogin(token)
        await this.getDict() // 缓存字典
        window.location.href = url
      } else {
        this.$message.warning(data.message)
      }
    }
  }
}
</script>
<style lang='scss'>
</style>
